#include "pch.hpp"

#include "hud.h"

const float SPECIAL_TIME = 2.0f;

void hud::draw(int rightBorder)
{
	if(!loaded)
	{
		std::cout << "[Hud::Draw] Load() first, you fool.\n";
		return;
	}

	int w = frames[frame].GetSubRect().GetWidth();
	int h = frames[frame].GetSubRect().GetHeight();

	float yFactor = 0;

	if(special && jumping)
	{
		yFactor = abs(sin(2*PI*hud_timer.GetElapsedTime()/SPECIAL_TIME*2.0f))*30.0f;
	}

	if(faceType == FaceType::Man)
	{
		frames[frame].SetPosition(sf::Vector2f(rightBorder - w, 768 - h / 2.0f - yFactor-20));
		podkladka_spr.SetPosition(sf::Vector2f(rightBorder - w, 768));
	}
	else if(faceType == FaceType::Woman)
	{
		frames[frame].SetPosition(sf::Vector2f(-rightBorder + w, 768 - h / 2.0f - yFactor));
		podkladka_spr.SetPosition(sf::Vector2f(-rightBorder + w, 768));
	}
	else
		std::cout << "[Hud::Draw] Wrong type, you fool.\n";

	app.Draw(podkladka_spr);
	app.Draw(frames[frame]);
}

void hud::load(FaceType::Type fType, int spriteWidth, int spriteHeight)
{
	faceType = fType;
	loaded = true;
	special = false;
	frame = 0;

	if(faceType == FaceType::Man)
		image.LoadFromFile("data/man.png");
	else if(faceType == FaceType::Woman)
		image.LoadFromFile("data/woman.png");
	else
		std::cout << "[Hud::Load] Wrong type, you fool.\n";

	frameCnt = image.GetWidth() / spriteWidth;

	for(int i = 0; i < frameCnt; ++i)
	{
		frames.push_back(sf::Sprite());
		frames.back().SetImage(image);
		frames.back().SetSubRect(sf::IntRect(i * spriteWidth, 0, (i + 1) * spriteWidth, spriteHeight));
		frames.back().SetCenter(spriteWidth / 2,spriteHeight / 2);
	}

	podkladka_img.LoadFromFile("data/podkladka.png");
	podkladka_spr.SetImage(podkladka_img);
	podkladka_spr.SetCenter(podkladka_img.GetWidth()/2.0f, podkladka_img.GetHeight());

	default_frame = 0;
}

void hud::step()
{
	if(special && hud_timer.GetElapsedTime() > SPECIAL_TIME)
	{
		frame = default_frame;
		special = false;
	}
}

void hud::setSpecialFrame(int frm, bool jump)
{
	if(frm + 1 > frameCnt)
	{
		std::cout << "[hud::setSpecialFrame] Wybrana klatka nie istnieje!" << std::endl;
		return;
	}

	frame = frm;
	hud_timer.Reset();
	special = true;
	jumping = jump;
}